<template>
  <div class="dashboard">
    <el-row :gutter="10">
      <el-col :span="7">
        <HyCard title="分类商品数量（饼图）">
          <PieEchart :pie-data="categoryGoodsCount" />
        </HyCard>
      </el-col>
      <el-col :span="10">
        <HyCard title="不同城市商品销量">
          <mapEchart :map-data="addressGoodsSale" />
        </HyCard>
      </el-col>
      <el-col :span="7">
        <HyCard title="分类商品数量（玫瑰图）">
          <roseEchart :rose-data="categoryGoodsCount" />
        </HyCard>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mr-to">
      <el-col :span="12">
        <HyCard title="分类商品销量">
          <lineEchart v-bind="categoryGoodsSale" />
        </HyCard>
      </el-col>
      <el-col :span="12">
        <HyCard title="分类商品收藏">
          <barEchart v-bind="categoryGoodsFavor" />
        </HyCard>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from '@/store'

import HyCard from '@/base-ui/card'
import { PieEchart, roseEchart, lineEchart, barEchart, mapEchart } from '@/components/page-echarts'

export default defineComponent({
  name: 'dashboard',
  components: { HyCard, PieEchart, roseEchart, lineEchart, barEchart, mapEchart },
  setup() {
    const store = useStore()
    store.dispatch('dashboard/getDashboardDataAction')
    // 饼图
    const categoryGoodsCount = computed(() => {
      return store.state.dashboard.categoryGoodsCount.map((item: any) => {
        return { name: item.name, value: item.goodsCount }
      })
    })

    const categoryGoodsSale = computed(() => {
      const xLabels: string[] = []
      const values: any[] = []

      const categoryGoodsSale = store.state.dashboard.categoryGoodsSale
      for (const item of categoryGoodsSale) {
        xLabels.push(item.name)
        values.push(item.goodsCount)
      }
      return { xLabels, values }
    })

    const categoryGoodsFavor = computed(() => {
      const xLabels: string[] = []
      const values: any[] = []
      const title = 'title字段'

      const categoryGoodsFavor = store.state.dashboard.categoryGoodsFavor
      for (const item of categoryGoodsFavor) {
        xLabels.push(item.name)
        values.push(item.goodsFavor)
      }
      return { xLabels, values, title }
    })

    const addressGoodsSale = computed(() => {
      return store.state.dashboard.addressGoodsSale.map((item: any) => {
        return { name: item.address, value: item.count }
      })
    })

    return { categoryGoodsCount, categoryGoodsSale, categoryGoodsFavor, addressGoodsSale }
  }
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}
.mr-to {
  margin-top: 20px;
}
</style>
